/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.util;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/5/12
 */
public class LocalCacheUtil {

    private static LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().maximumSize(1000)
            .expireAfterWrite(600, TimeUnit.SECONDS).recordStats().build(new CacheLoader<Object, Object>() {
                @Override
                public Object load(Object key) throws Exception {
                    return key;
                }
            });

    public static Object get(Object key) {
        try {
            Object var = cache.get(key);
            if (var.equals(key)) {
                return null;
            } else {
                return var;
            }
        }catch (ExecutionException e){
            return null;
        }
    }

    public static void put(Object key, Object value) {
        cache.put(key, value);
    }

    public static void remove(Object key) {
        cache.invalidate(key);
    }
}
